package com.bw.zg3park2212.user

import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.databinding.ActivityRegiserBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Route(path = "/app/RegiserActivity")
@AndroidEntryPoint
class RegiserActivity : BaseActivity<ActivityRegiserBinding,UserViewModel>() {
    override fun intitView() {
        binding.btnCode.setOnClickListener {
            //随机数
            val code = (1000..9999).random()
            SPUtils.getInstance().put("code",code.toString())
            ToastUtils.showLong("验证码为：$code")
            Log.i("验证码",code.toString())
            lifecycleScope.launch {
                for (i in 10 downTo 0){
                    delay(1000)
                    binding.btnCode.text="$i 秒后重新获取"
                    binding.btnCode.isEnabled=false
                }
                binding.btnCode.isEnabled=true
                binding.btnCode.text="获取验证码"
            }

        }
        binding.register.setOnClickListener {
            if (binding.edCode.text.toString().isEmpty()){
                ToastUtils.showLong("请输入验证码")
                return@setOnClickListener
            }
            if (!binding.edCode.text.toString().equals(SPUtils.getInstance().getString("code"))){
                ToastUtils.showLong("验证码不一致")
                return@setOnClickListener
            }
            if (!binding.password.text.toString().equals(binding.repassword.text.toString())){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            viewmodel.regiser(binding.phone.text.toString(),binding.password.text.toString())
        }
        binding.tvGotologin.setOnClickListener {
            ARouter.getInstance().build("/app/LoginActivity").navigation()
            finish()
        }
    }

    override fun intitData() {
        viewmodel.regiserSuccse.observe(this){
            Log.i("===",it.toString())
            ToastUtils.showLong("注册成功")
            finish()
        }
    }

}